package com.mdp.im.core.subscriber.listener;

import com.mdp.im.core.scbscriber.api.ImChannelConfig;
import com.mdp.im.core.utils.ImTools;
import com.mdp.im.core.utils.TopicTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;

@Configuration 
public class ImChannelCfg implements ImChannelConfig {
	RedisMessageListenerContainer container;
	
	@Autowired
	ImMessageListener messageListener;

	@Override
	public void container(Object container) {
		this.container=(RedisMessageListenerContainer)container;

		/**
		 * 私聊消息只监听发往本服务器的消息
		 */
		this.container.addMessageListener(messageListener, topic(TopicTools.getPrichatTopicName(ImTools.getHostAddress())));
		this.container.addMessageListener(messageListener, topic( TopicTools.getSysTopicName()));

	}

	public void setMessageListener(MessageListener messageListener, Topic topic){
		container.addMessageListener(messageListener, topic);
	}

	public void addMessageListener(String groupId){
		container.addMessageListener(messageListener, topic(TopicTools.getGroupTopicName(groupId)));
	}
	@Override
	public Topic topic(String channelName){
		ChannelTopic topic=new ChannelTopic(channelName);
		return topic;
		
	}

	public void removeMessageListener(MessageListener messageListener, Topic topic) {
		container.removeMessageListener(messageListener,topic);
		
	}

	public void removeMessageListener(MessageListener messageListener) {
		container.removeMessageListener(messageListener);
		
	}

	public void removeMessageListener(Topic topic) {

		container.removeMessageListener(messageListener,topic);
	}

	public void addMessageListener(Topic topic) {
		container.addMessageListener(messageListener,topic);
	}

	public void removeMessageListener(String groupId) {
		container.removeMessageListener(messageListener, topic(TopicTools.getGroupTopicName(groupId)));

	}
}
